package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.RoadmapBigType;
import com.sailfish.springbootdemo.pojo.db1.RoadmapParam;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface RoadmapParamDao {

    /**
     * 查询
     *
     * @param searchValue
     * @return
     */
    @Results(id = "roadmapParamResultMap", value = {
            @Result(column = "roadmap_param_id", property = "roadmapParamId", jdbcType = JdbcType.INTEGER),
            @Result(column = "roadmap_param_name", property = "roadmapParamName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_big_type_id", property = "roadmapBigTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "roadmap_small_type_id", property = "roadmapSmallTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "roadmap_param_notes", property = "roadmapParamNotes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_param_picture", property = "roadmapParamPicture", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_param_type", property = "roadmapParamType", jdbcType = JdbcType.INTEGER),
            @Result(column = "roadmap_param_placeholder", property = "roadmapParamPlaceholder", jdbcType = JdbcType.VARCHAR),
            @Result(column = "roadmap_param_options", property = "roadmapParamOptions", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_required", property = "isRequired", jdbcType = JdbcType.INTEGER),
            @Result(column = "roadmap_param_key", property = "roadmapParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "value_algorithm_type", property = "valueAlgorithmType", jdbcType = JdbcType.INTEGER),
            @Result(column = "tech_device_param_keys", property = "techDeviceParamKeys", jdbcType = JdbcType.VARCHAR),
            @Result(column = "rule_algorithm_type", property = "ruleAlgorithmType", jdbcType = JdbcType.INTEGER),
            @Result(column = "key_from_type", property = "keyFromType", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            " select * from " + DB1TableName.ROADMAP_PARAM + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and (roadmap_param_name like CONCAT('%',#{searchValue},'%'))" +
            "</if>" +
            "<if test='roadmapBigTypeId != null and roadmapBigTypeId != \"\" '>" +
            " and roadmap_big_type_id = #{roadmapBigTypeId} " +
            "</if>" +
            "<if test='roadmapSmallTypeId != null and roadmapSmallTypeId != \"\" '>" +
            " and roadmap_small_type_id = #{roadmapSmallTypeId} " +
            "</if>" +
            " order by sort" +
            "</script>")
    List<RoadmapParam> getAllRoadmapParam(@Param("searchValue") String searchValue, @Param("roadmapBigTypeId") Integer roadmapBigTypeId, @Param("roadmapSmallTypeId") Integer roadmapSmallTypeId);

    @ResultMap(value = "roadmapParamResultMap")
    @Select("select * from " + DB1TableName.ROADMAP_PARAM + " where roadmap_param_id = #{roadmapParamId}")
    RoadmapParam getRoadmapParamById(@Param("roadmapParamId") Integer roadmapParamId);

    @ResultMap(value = "roadmapParamResultMap")
    @Select("select * from " + DB1TableName.ROADMAP_PARAM + " where roadmap_param_key = #{roadmapParamKey}")
    RoadmapParam getRoadmapParamByKey(@Param("roadmapParamKey") String roadmapParamKey);

    /**
     * 删除
     *
     * @param roadmapParamId
     * @return
     */
    @Delete("delete from " + DB1TableName.ROADMAP_PARAM + " where roadmap_param_id = #{roadmapParamId}")
    Integer deleteRoadmapParam(@Param("roadmapParamId") Integer roadmapParamId);

    /**
     * 新增
     *
     * @param roadmapParam
     * @return
     */
    @Insert("insert into " + DB1TableName.ROADMAP_PARAM + "(roadmap_param_name,roadmap_big_type_id,roadmap_small_type_id,status,sort,create_time,roadmap_param_notes,roadmap_param_picture,roadmap_param_type,roadmap_param_placeholder,roadmap_param_options,is_required,roadmap_param_key) values " +
            "(#{roadmapParam.roadmapParamName},#{roadmapParam.roadmapBigTypeId},#{roadmapParam.roadmapSmallTypeId},#{roadmapParam.status},#{roadmapParam.sort},#{roadmapParam.createTime},#{roadmapParam.roadmapParamNotes},#{roadmapParam.roadmapParamPicture},#{roadmapParam.roadmapParamType},#{roadmapParam.roadmapParamPlaceholder},#{roadmapParam.roadmapParamOptions},#{roadmapParam.isRequired},#{roadmapParam.roadmapParamKey})")
    Integer addRoadmapParam(@Param("roadmapParam") RoadmapParam roadmapParam);

    /**
     * 更新
     *
     * @param roadmapParam
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.ROADMAP_PARAM +
            "<set>" +
            "<if test='roadmapParam.roadmapParamName != null and roadmapParam.roadmapParamName != \"\" '>" +
            " roadmap_param_name = #{roadmapParam.roadmapParamName}," +
            "</if>" +
            "<if test='roadmapParam.roadmapBigTypeId != null and roadmapParam.roadmapBigTypeId != \"\" '>" +
            " roadmap_big_type_id = #{roadmapParam.roadmapBigTypeId}," +
            "</if>" +
            "<if test='roadmapParam.roadmapSmallTypeId != null and roadmapParam.roadmapSmallTypeId != \"\" '>" +
            " roadmap_small_type_id = #{roadmapParam.roadmapSmallTypeId}," +
            "</if>" +
            "<if test='roadmapParam.roadmapParamNotes != null and roadmapParam.roadmapParamNotes != \"\" '>" +
            " roadmap_param_notes = #{roadmapParam.roadmapParamNotes}," +
            "</if>" +
            "<if test='roadmapParam.roadmapParamPicture != null and roadmapParam.roadmapParamPicture != \"\" '>" +
            " roadmap_param_picture = #{roadmapParam.roadmapParamPicture}," +
            "</if>" +
            "<if test='roadmapParam.roadmapParamType != null and roadmapParam.roadmapParamType != \"\" '>" +
            " roadmap_param_type = #{roadmapParam.roadmapParamType}," +
            "</if>" +
            "<if test='roadmapParam.roadmapParamPlaceholder != null '>" +
            " roadmap_param_placeholder = #{roadmapParam.roadmapParamPlaceholder}," +
            "</if>" +
            "<if test='roadmapParam.roadmapParamKey != null and roadmapParam.roadmapParamKey != \"\" '>" +
            " roadmap_param_key = #{roadmapParam.roadmapParamKey}," +
            "</if>" +
            "<if test='roadmapParam.updateTime != null '>" +
            " update_time = #{roadmapParam.updateTime}," +
            "</if>" +
            "<if test='roadmapParam.sort != null and roadmapParam.sort != \"\" '>" +
            " sort = #{roadmapParam.sort}," +
            "</if>" +
            "<if test='roadmapParam.status != null and roadmapParam.status != \"\" '>" +
            " status = #{roadmapParam.status}," +
            "</if>" +
            "<if test='roadmapParam.roadmapParamOptions != null and roadmapParam.roadmapParamOptions != \"\" '>" +
            " roadmap_param_options = #{roadmapParam.roadmapParamOptions}," +
            "</if>" +
            "<if test='roadmapParam.isRequired != null and roadmapParam.isRequired != \"\" '>" +
            " is_required = #{roadmapParam.isRequired}," +
            "</if>" +
            "</set>" +
            " where roadmap_param_id = #{roadmapParam.roadmapParamId}" +
            "</script>")
    Integer updateRoadmapParam(@Param("roadmapParam") RoadmapParam roadmapParam);

    /**
     * 更新rule
     *
     * @param roadmapParamId
     * @param valueAlgorithmType
     * @param techDeviceParamKeys
     * @return
     */
    @Update("update " + DB1TableName.ROADMAP_PARAM + " set value_algorithm_type = #{valueAlgorithmType},tech_device_param_keys = #{techDeviceParamKeys}, rule_algorithm_type = #{ruleAlgorithmType},key_from_type = #{keyFromType} where roadmap_param_id = #{roadmapParamId}")
    Integer updateRoadmapRule(@Param("roadmapParamId") Integer roadmapParamId, @Param("valueAlgorithmType") Integer valueAlgorithmType, @Param("techDeviceParamKeys") String techDeviceParamKeys, @Param("ruleAlgorithmType") Integer ruleAlgorithmType, @Param("keyFromType") Integer keyFromType);
}
